package qeug.shsh.fi.fromPlugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.epm.eb.formplugin.AbstractListPlugin;
import qeug.shsh.helper.FacadeHelper;
import qeug.shsh.webService.WebServiceUtil;

import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.EventObject;
import java.util.SortedMap;
import java.util.TreeMap;

public class TestFromPlugin extends AbstractListPlugin {
    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        try {
        SortedMap<String, Object> map = new TreeMap<String, Object>();
        ListSelectedRowCollection selectedRows = this.getSelectedRows();
        ListSelectedRow listSelectedRow = selectedRows.get(0);
        String billNo = listSelectedRow.getBillNo();
        DynamicObject bill = BusinessDataServiceHelper
                .loadSingle("er_tripreimbursebill", new QFilter("billno", "=", billNo).toArray());
        map.put("ERPID", bill.get("id"));//唯一标识符
        map.put("ERPNO", bill.get("billno"));//唯一标识编号
        map.put("FMCLSNO", "EPMS004");//付款业务类型
        map.put("CURRENCYNO", bill.getDynamicObjectCollection("accountentry").
                get(0).getDynamicObject("accountcurrency").get("number"));//币种
        map.put("PAYTYPE", bill.get("qeug_paytype"));//付款类别
        map.put("PAYCLTNAME", bill.getDynamicObject("costdept").get("name"));//付款单位名称
        map.put("CUSTNAME", bill.getDynamicObject("costcompany").get("name"));//付款所属部门名称
        map.put("RECACTNO", bill.getDynamicObjectCollection("accountentry").get(0).get("payeraccount"));//收款方账号
        map.put("RECNAME", bill.getDynamicObjectCollection("accountentry").get(0).get("payeraccountname"));//收款方户名
        map.put("RECBANKNO", bill.getDynamicObjectCollection("accountentry").
                get(0).getDynamicObject("payerbank").get("number"));//收款方支行编码
        map.put("RECBANKNAME", bill.getDynamicObjectCollection("accountentry").get(0).
                getDynamicObject("payerbank").get("name"));//收款方支行名称
        map.put("RELCORPNO", "EPMS004");//客商编码
        map.put("PAYACTDATE", bill.getDate("repaymentdate"));//期望付款日期
        map.put("AMOUNT", bill.getBigDecimal("approveamount"));//付款金额
        map.put("ISPERSON", 1);//对公对私标识
        map.put("EXPLAIN", bill.get("description"));//摘要（用途）
//        map.put("KMNAME", bill.getDynamicObject("tripexpenseitem").get("name"));//款项用途（名称）
            map.put("KMNAME", "差旅费");//款项用途（名称）

//            map.put("ITEMTID","EPMS004");//资金计划行项目号
        map.put("PAYINNER", 0);//是否内转
        map.put("URGENTFLAG", 0);//加急标识
//            map.put("CONTRACTNO",0);//业务合同编号
//            map.put("MEMO",bill.getDynamicObjectCollection("").get(0).get(""));//备注
        map.put("PAYPERSON", bill.getDynamicObject("applier").get("name"));//付款经办人
        map.put("PAYDATE", bill.getDate("bizdate"));//制单日期
        map.put("CHANNEL", "CCS");//付款单发起渠
        map.put("SETTLEMENTMODES", "0");//结算方式
        map.put("STCODE", bill.get("qeug_paytype"));//付款方式
        map.put("BUDGETFLAG", bill.get("qeug_budgetflag"));//预算类型
        map.put("FMCLS", 2);//业务类型
        map.put("ISWAIT", 0);//是否等通知
//            map.put("PROJECTNAME",0);//投资项目
        map.put("DIR", 0);//主/被动付款
        map.put("DOCTYPE", 2);//单据类型
        String toXml = FacadeHelper.mapToXml(map, "data", "item");
        String xml = FacadeHelper.perfectXml(toXml, bill);
        String json = null;
            json = WebServiceUtil.invokeG6Pay("http://172.30.18.188:7081/FDLKF_OUTER/webservice/TxServiceGateway?wsdl", xml);
        System.out.println();
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        } catch (ServiceException e) {
            throw new RuntimeException(e);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("baritemap1");
    }
}
